const User = require("../schema/user");
// 用户登录
const login = (username, password) => {
    return User.findOne({ username, password });
};
// 查询用户
const find = (query, fields = "") => {
    return User.findOne(query).select(fields);
};
// 注册用户
const register = (username, password) => {
    return User.create({ username, password });
};

// 用户详情
const detail = (id, selects) => {
    return User.findById(id).select(selects);
};

// 更新用户信息
const update = (id, data) => {
    return User.findByIdAndUpdate(id, data);
};

// 用户关注列表
const follow = (id) => {
    return User.findById(id).select("+following").populate("following");
};

// 用户关注话题列表
const followingTopics = (id) => {
    return User.findById(id)
        .select("+followingTopics")
        .populate("followingTopics");
};

// 用户粉丝列表
const followers = (id) => {
    return User.find({ following: id });
};

// 关注某个话题的用户列表
const followingTopicUsers = (id) => {
    return User.find({ followingTopics: id });
};

module.exports = {
    login,
    register,
    find,
    detail,
    update,
    follow,
    followers,
    followingTopics,
    followingTopicUsers,
};
